<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动ID" prop="activityId">
        <el-input v-model="queryParams.activityId" placeholder="请输入活动ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="心得作者" prop="author">
        <el-input v-model="queryParams.author" placeholder="请输入作者" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="发布时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeReleaseTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:experience:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:experience:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:experience:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:experience:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table table-layout="auto" v-loading="loading" :data="experienceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属活动ID" align="center" prop="activityId" />
      <el-table-column label="作者" align="center" prop="author" />
      <!-- <el-table-column label="内容" align="center" prop="content">
        <template #default="scope">
          <el-link
            @click="proxy.$modal.alert(scope.row.content, `${scope.row.author}的心得体会`)"
            type="primary"
            :underline="false"
            >查看内容</el-link
          >
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="心得附件" align="center" prop="file">
        <template #default="scope">
          <el-link v-if="scope.row.file" :href="scope.row.file" target="_blank">{{ scope.row.file }}</el-link>
          <span v-else>无</span>
        </template>
      </el-table-column> -->
      <el-table-column label="发布时间" align="center" prop="releaseTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.releaseTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:experience:edit']">修改</el-button> -->
          <el-button
            link
            type="primary"
            icon="View"
            @click="proxy.$modal.alert(scope.row.content, `${scope.row.author}的心得体会`)"
            >预览</el-button
          >
          <el-button link type="primary" icon="Download" :disabled="!scope.row.file" @click="download.resource(scope.row.file)"
            >附件</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:experience:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改支部活动心得对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="experienceRef" :model="form" :rules="rules" label-width="80px"> </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Experience">
import { listExperience, getExperience, delExperience, addExperience, updateExperience } from "@/api/system/experience";
import download from "@/plugins/download";

const { proxy } = getCurrentInstance();

const experienceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeReleaseTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activityId: null,
    author: null,
    releaseTime: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询支部活动心得列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeReleaseTime && "" != daterangeReleaseTime) {
    queryParams.value.params["beginReleaseTime"] = daterangeReleaseTime.value[0];
    queryParams.value.params["endReleaseTime"] = daterangeReleaseTime.value[1];
  }
  listExperience(queryParams.value).then((response) => {
    experienceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    activityId: null,
    author: null,
    content: null,
    file: null,
    releaseTime: null,
  };
  proxy.resetForm("experienceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeReleaseTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加支部活动心得";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getExperience(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改支部活动心得";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["experienceRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateExperience(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addExperience(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除支部活动心得编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delExperience(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/experience/export",
    {
      ...queryParams.value,
    },
    `experience_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
